package cap.dao.impl;

import cap.bean.ChatVo;
import cap.dao.ChatDao;
import cap.db.DBPool;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ChatDaoImpl implements ChatDao {
    Connection conn=null;
    PreparedStatement ps=null;
    ResultSet that=null;
    @Override
    public void speaking(ChatVo chatVo) throws SQLException {
        conn= DBPool.getConn();
        String sql="insert into chatting(speaker,speaking,room) values(?,?,?)";
        Integer res = 0;
        ps = conn.prepareStatement(sql);
        ps.setString(1,chatVo.getSpeaker());
        ps.setString(2,chatVo.getSpeaking());
        ps.setInt(3,chatVo.getRoom());
        res = ps.executeUpdate();
        DBPool.close(null,ps,conn);
    }

    @Override
    public List<ChatVo> reading(String room) throws SQLException {
        conn=DBPool.getConn();
        String sql = "select * from chatting where room=? limit 20";
        List<ChatVo> res = new ArrayList<ChatVo>();
        ps = conn.prepareStatement(sql);
        ps.setInt(1, Integer.parseInt(room));
        that = ps.executeQuery();
        while(that.next()){
            ChatVo it = new ChatVo();
            it.setSpeaker(that.getString("speaker"));
            it.setSpeaking(that.getString("speaking"));
            res.add(it);
        }
        DBPool.close(that,ps,conn);
        return res;
    }

    @Override
    public void attending(String nickname, String room) throws SQLException {
        conn=DBPool.getConn();
        String sql = "insert into chatting(speaker,speaking,room) values(?,?,?)";
        ps = conn.prepareStatement(sql);
        ps.setString(1,nickname);
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ps.setString(2,nickname+" 进来了------"+format.format(new Date()));
        ps.setInt(3, Integer.parseInt(room));
        ps.executeUpdate();
        String sql1 = "update chatterroom set roomonline=roomonline+1 where id=?";
        PreparedStatement ps1 = conn.prepareStatement(sql1);
        ps1.setInt(1,Integer.parseInt(room));
        ps1.executeUpdate();
        ps1.close();
        DBPool.close(that,ps,conn);
    }

    @Override
    public void exit(String nickname, String room) throws SQLException {
        conn=DBPool.getConn();
        String sql = "insert into chatting(speaker,speaking,room) values(?,?,?)";
        ps = conn.prepareStatement(sql);
        ps.setString(1,nickname);
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ps.setString(2,nickname+" 离开了------"+format.format(new Date()));
        ps.setString(3,room);
        ps.executeUpdate();
        String sql1 = "update chatterroom set roomonline=roomonline-1 where id=?";
        PreparedStatement ps1 = conn.prepareStatement(sql1);
        ps1.setInt(1, Integer.parseInt(room));
        ps1.executeUpdate();
        ps1.close();
        DBPool.close(that,ps,conn);
    }
}
